import axios from 'axios'
import qs from 'qs'
import VueCookies from 'vue-cookies'

axios.defaults.withCredentials=true;

function req(type, url, params, success, fail) {
	//VueCookies.set('token','c725093b8e6ca698ac204c7c296458d1');
	var urlStr;
		if(process.env.NODE_ENV=="development"){
			urlStr="https://www.fastmock.site/mock/9655937256c5377d234110e4279ddfc5/api"+url;  //测试
		}else if(process.env.NODE_ENV=="production"){
			//urlStr="https://admin.hzcckj.cn"+url;  //正式
		}
	
	//alert(document.cookie);
	axios({
		method: type, 
		url: urlStr,
		headers: {'token':localStorage.getItem('token')},
		params: type == "get" ? qs.stringify(params) : '',
		data: type == "post" ? qs.stringify(params) : '',
	}).then(function(res) {
		if (success) {
			success(res.data);
		} else if (failure) {
			failure(res.data);
		} else {
			console.log('error: ' + JSON.stringify(res.data));
		}
	})
}



export default {
	get: function(url, params, success, fail) {
		return req('get', url, params, success, fail);
	},
	post: function(url, params, success, fail) {
		return req('post', url, params, success, fail);
	}
}
